package com.fortune.service.impl;

import com.fortune.dao.PaymentMgtDao;
import com.fortune.dto.in.paymentMgt.DistributorPaymentAddDto;
import com.fortune.dto.in.paymentMgt.DistributorPaymentEditDto;
import com.fortune.dto.in.paymentMgt.DistributorPaymentListDto;
import com.fortune.dto.in.paymentMgt.OutPayId;
import com.fortune.exception.db.DeleteInnerErrorException;
import com.fortune.exception.db.InsertInnerErrorException;
import com.fortune.exception.db.UpdateInnerErrorException;
import com.fortune.service.PaymentMgtService;
import com.fortune.util.UUIDUtil;
import com.fortune.vo.paymentMgt.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/5/29 0029.
 */
@Service
public class PaymentMgtServiceImpl implements PaymentMgtService {
    @Autowired
    private PaymentMgtDao dao;
    @Override
    public List<DistributorPayment> findDistributorPaymentList(DistributorPaymentListDto dto, String mid) {
        return dao.findDistributorPaymentList(dto,mid);
    }

    @Override
    public int findDistributorPaymentListCount(DistributorPaymentListDto dto, String mid) {
        return dao.findDistributorPaymentListCount(dto,mid);
    }

    @Override
    public DistributorPaymentDetail findDetail(String mid, String paymentId) {
        DistributorPaymentDetail detail = dao.findDetail(mid,paymentId);
        List<OutPay> list = dao.findOutPay(mid,detail.getId());
        detail.setOutPay(list);
        return detail;
    }

    @Override
    @Transactional
    public int add(String userId,DistributorPaymentAddDto addDto,String mid,String id) {
        int success = dao.add(userId,addDto,mid,addDto.getDid(),id);
        if(success > 0){
          List<OutPayId> list = addDto.getOutId();
          for(OutPayId ids: list){
             int update = dao.updateOutPay(mid,addDto.getDid(),ids.getOutPayId(),addDto.getPayTypeId(),id);
             if(update <= 0){
                 throw new InsertInnerErrorException("添加失败");
             }
          }
        }
        return success;
    }

    @Override
    @Transactional
    public int update(String userId,DistributorPaymentEditDto editDto, String mid, String did) {
     /*   int delete = dao.delete(editDto,mid,did);*/
        int updates = dao.updatePrice(mid,did,editDto);
        if(updates > 0){
            List<OutPay> list = dao.findOutPay(mid,editDto.getId());
            for(OutPay outPay : list){
                int update = dao.restartPay(mid,outPay.getId(),did);
                if(update < 1){
                    throw new UpdateInnerErrorException("编辑失败");
                }
            }
            List<OutPayId> lists = editDto.getOutId();
            for(OutPayId ids: lists){
                int update = dao.updateOutPay(mid,did,ids.getOutPayId(),editDto.getPayTypeId(),editDto.getId());
                if(update <= 0){
                    throw new InsertInnerErrorException("编辑失败");
                }
            }
        }else{
            throw new InsertInnerErrorException("编辑失败");
        }
        return 1;

/*        if(delete > 0){
            int success = dao.updateAdd(userId,editDto,mid,did,UUIDUtil.get32UUID());
            if(success > 0 ){
                List<OutPayId> list = editDto.getOutId();
                for(OutPayId ids: list){
                    int update = dao.updateOutPay(mid,did,ids.getOutPayId(),editDto.getPayTypeId());
                    if(update <= 0){
                        throw new InsertInnerErrorException("添加失败");
                    }
                }
                return 1;
            }else{
                throw new InsertInnerErrorException("添加失败");
            }
        }else{
            throw new DeleteInnerErrorException("删除失败");
        }*/
    }

    @Override
    public int deletePayment(String mid, String paymentId) {
        return dao.deletePayment(mid,paymentId);
    }

    @Override
    public List<MOutInfoInPaymentMgt> findOutList(String mid, String distributorId) {
        return dao.findOutList(mid,distributorId);
    }

    @Override
    public List<MOutInfoInPaymentMgt> findOutListWhereId(String mid, String distributorId, String id) {
        List<MOutInfoInPaymentMgt> outList = new ArrayList<>();
        List<MOutInfoInPaymentMgt> list = dao.findOutLists(mid,distributorId);
        List<MOutInfoInPaymentMgt> list2 = dao.findOwnArrears(mid,id);
        if(list2.size() > 0){
            for(MOutInfoInPaymentMgt mOutInfoInPaymentMgt1 : list2){
                outList.add(mOutInfoInPaymentMgt1);
            }
        }
        for(MOutInfoInPaymentMgt mOutInfoInPaymentMgt:list){
            if(mOutInfoInPaymentMgt.getIsConfirm().equals("NO")) {
                outList.add(mOutInfoInPaymentMgt);
            }
        }
        for(MOutInfoInPaymentMgt mOutInfoInPaymentMgt2:outList){
            if(mOutInfoInPaymentMgt2.getDpid() != null) {
                if (mOutInfoInPaymentMgt2.getDpid().equals(id)) {
                    mOutInfoInPaymentMgt2.setPayStatus(true);
                }
            }
        }
        return outList;
    }

    @Override
    public List<DidPayment> findDid(String mid) {
        return dao.findDid(mid);
    }

    @Override
    public List<DidPayment> findDidUser(String did) {
        return dao.findDidUser(did);
    }

    @Override
    public List<DidPayment> findPayType() {
        return dao.findPayType();
    }
}
